package com.common.Util;

import cn.hutool.crypto.SecureUtil;
import com.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: Member_Perks_Store
 * @description: 字符串操作工具类
 * @author: 无恙
 * @create: 2024-06-17 13:29
 **/


public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private static final Logger log = LoggerFactory.getLogger(StringUtils.class);

    public static String getStringOrDefault(String src,String defaultValue){
        return isBlank(src)?defaultValue:src;
    }

    public static List<String> listBySplit(String text, String separator) {
        if(text == null) return new ArrayList<>();
        String[] ss = split(text,separator);
        return Arrays.asList(ss);
    }

    //校验手机号
    public static boolean isValidPhone(String phone) {
        if(isBlank(phone)) return false;
      return phone.matches(Constants.VALID_PHONE_PATTERN);
    }

    //校验邮箱
    public static boolean isValidEmail(String email) {
        if(isBlank(email)) return false;
        return email.matches(Constants.VALID_EMAIL_PATTERN);
    }

    //生成指定位数随机数
    public static String RandomNumber(int length) {
        String str = "0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = (int) (Math.random() * 10);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    //加密工具MD5
    public static String MD5(String str) {
        return  SecureUtil.md5(str);
    }



}
